package cn.example.web.db.dao;

import org.artifact.core.db.MySqlDao;
import org.artifact.core.lang.StrPlus;
import java.util.List;
import java.util.ArrayList;
import java.text.MessageFormat;
import java.util.function.BiFunction;
import org.artifact.core.db.BaseBean;
import cn.hutool.core.map.MapUtil;
import java.util.Map;
import java.util.HashMap;
import org.artifact.core.db.BaseDao;
import cn.example.web.db.bean.Server;
import static cn.hutool.core.util.ObjectUtil.equal;

public abstract class ServerDAO extends MySqlDao<Server>{
	
	protected final String sqlAllColumn = "id, name, tag, status, min_client_version, max_client_version, create_date, modified_date";

	protected final String sqlAllPla = "?,?,?,?,?,?,?,?";

	@Override
	protected String getDBName() {
		return "example_web";
	}
	
	@Override
	protected String getTableName() {
		return "t_server";
	}
	
	@Override
	protected Class<Server> getEntityClass(){
		return Server.class;
	}
	
	@Override
	protected Server createEntity() {
		return new Server() {};
	}

	@Override
	protected String getPKFieldName() {
		return "id";
	}
	
	// 根据主键导出
	public Map<String,Object> exportByPrimaryKey(long id,BiFunction<BaseDao,BaseBean,Object> function){
		return export(findByPrimaryKey(id),function);
	}
	
	
	public Map<String,Object> export(Server server,BiFunction<BaseDao,BaseBean,Object> function){
		Map<String,Object> data = new HashMap<>();
		data.put("self", function.apply(this,server));
		return data;
	}
	
	public List<Map<String,Object>> export(List<Server> servers,BiFunction<BaseDao,BaseBean,Object> function){
		List<Map<String,Object>> data = new ArrayList<>();
		for(Server server : servers){
			data.add(export(server,function));
		}
		return data;
	}


	public Server _import(Map<String,Object> data,BiFunction<BaseDao,Map,BaseBean> function){
	    Server server = createEntity();
	    server = (Server)function.apply(this,MapUtil.get(data,"self",Map.class));

        return server;
    }

    public List<Server> _import(List<Map<String,Object>> data,BiFunction<BaseDao,Map,BaseBean> function){
        List<Server> list = new ArrayList<>();
        for(Map<String,Object> map : data){
            list.add(_import(map,function));
        }
        return list;
    }
	
	
	@Override
	public boolean createTable(){
		StrPlus plus = StrPlus.b("CREATE TABLE IF NOT EXISTS `t_server` (");
		plus.a("  `id` bigint(20) NOT NULL COMMENT '主键',");
		plus.a("  `name` varchar(45) NOT NULL COMMENT '服务器名称',");
		plus.a("  `tag` int(11) NOT NULL COMMENT '标记:0.默认 1.推荐  2.新服  3.火爆  ',");
		plus.a("  `status` int(11) NOT NULL COMMENT '状态:0.未激活 1.激活  2.维护  3.运行  ',");
		plus.a("  `min_client_version` varchar(45) NOT NULL COMMENT '最小客户端版本号',");
		plus.a("  `max_client_version` varchar(45) NOT NULL COMMENT '最大客户端版本号',");
		plus.a("  `create_date` datetime NOT NULL COMMENT '创建时间',");
		plus.a("  `modified_date` datetime NOT NULL COMMENT '修改时间',");
		plus.a("  PRIMARY KEY (`id`)");
		plus.a(") ENGINE=InnoDB DEFAULT CHARSET=utf8  COMMENT='服务器';");
		update(plus.e());
		return true;
	}
	
	@Override
	protected String getSaveSql() {
		return MessageFormat.format("REPLACE INTO {0} ({1}) VALUES ({2})",getTableName(),sqlAllColumn,sqlAllPla);
	}

	@Override
	protected String getDeleteSql() {
		return MessageFormat.format("DELETE FROM {0} WHERE id = ?",getTableName());
	}
	
}